<!--
PAGE DOCUMENTATION:
==================
This page is where users can invite (send FB invitaions) friends to add the application.
All users are directed here after adding the application for the 1st time.
It is in this page (if the application was just added) that the users infinite session key
(given by FB) is extracted and saved to our DB.

Parameters that can be sent to this page:
----------------------------------------
auth_token - if the user just added the application, this is the indication of it.
-->

<html>
<fb:dashboard>
<fb:action href="getMessages.php">
Hear messages sent to you!
</fb:action>
<fb:action href="sendMessage.php">
Send a voice message to a friend!
</fb:action>
<fb:action href="index.php">
VoiceMessanger main page!
</fb:action>
<fb:help href="help.php">
Help Me!
</fb:help>
</fb:dashboard>

<?php
require_once 'appinclude.php';
require_once 'logsLib.php';
require_once 'sqlLib.php';

$currentPage = "invite";
$user = $facebook->require_login();

if (isset($_GET['auth_token'])) { //app was just added - get session key.
 try {
  $validation = $facebook->validate_fb_params();
 } catch (Exception $ex) {
  documentError($user_id,$currentPage,"FaceBook Exception: ".$ex);
 }
 if (!$validation) {
  echo "<fb:redirect url=\"error.php?action=invite\" />";
 } else {
  $session_key = $facebook->fb_params['session_key']; 
  $sqlDb = new mysqli("localhost", "root", "", "messages");
  /* check connection */
  if (mysqli_connect_errno()) {
      echo "<fb:redirect url=\"error.php?action=invite\" />";
  } else {
   if (!storeSessionKey($sqlDb,$user,$session_key)) {
    echo "<fb:redirect url=\"error.php?action=invite\" />";
   }
   $sqlDb->close();
  }
 }
}


//  Get list of friends who don't have this app installed.
try {
	$result = $facebook->api_client->fql_query("SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = $user)");
} catch (Exception $ex) {
	echo "<fb:redirect url=\"error.php?action=invite\" />";
}
logFunctionCall($user, "facebook->api_client->fql_query","SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = $user)", $result,$currentPage);

$friendsList = "";

//  Build a delimited list of users...
if ($result)
{
	for ( $i = 0; $i < count($result); $i++ )
	{
		if ( $friendsList != "" )
			$friendsList .= ",";
	
		$friendsList .= $result[$i]["uid"];
	}
}

//  Build invite text
$invfbml = <<<FBML
You've been invited to add Voice Messenger!
<fb:name uid="$user" firstnameonly="true" shownetwork="false"/> wants you to add Voice Messenger so that you can hear <fb:pronoun possessive="true" uid="$user"/> messages and send some, too!
<fb:req-choice url="http://apps.facebook.com/voicemessenger/index.php" label="Add Voice Messenger!" />
FBML;

?>

<!-- create FB multi-friends invite -->
<fb:request-form type="voiceMessenger" action="index.php" content="<?php echo htmlentities($invfbml);?>" invite="true">
	<fb:multi-friend-selector 
	max="20" 
	actiontext="Here are your friends who don't have VoiceMessenger. Invite them to hear you out!" 
	showborder="true" 
	rows="5" 
	exclude_ids="<?php echo $friendsList;?>">
</fb:request-form>

<fb:swf 
	swfsrc='http://64.22.126.39/flashVersion.swf' 
	width='550' height='200'
	align = 'center'
	/>
	
</br>
<font size="3">
This application participates in Unicell's contest. <a href="http://www.liveface.co.il/%D7%AA%D7%97%D7%A8%D7%95%D7%AA-%D7%90%D7%A4%D7%9C%D7%99%D7%A7%D7%A6%D7%99%D7%95%D7%AA-%D7%A4%D7%99%D7%99%D7%A1%D7%91%D7%95%D7%A7/apps/" > For rating and other applications. </a>
</font>